package com.mytrip.webapp.action.login;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.mytrip.base.action.StrutsActionAdapter;
import com.mytrip.model.Userinfo;
import com.mytrip.service.UserManager;
import com.mytrip.util.LoginUtil;
import com.mytrip.webapp.filter.CtxSessionBag;

public class FrontLoginAction extends StrutsActionAdapter{

	private static final long serialVersionUID = 425710998349788906L;

	private UserManager userManager;

	// ~ method =================================================================

	public String view() throws Exception{
		// mcSites = McSiteConfigFactory.getInstance().findSites();
		return SUCCESS;
	}

	public String login() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		Userinfo userinfo = userManager.findUserByEmailOrMobile(request.getParameter("username"), request
				.getParameter("password"));

		CtxSessionBag.getSessionBag().setUserinfo(userinfo);
		
		LoginUtil.saveCookie(userinfo.getEmail(), userinfo.getPwd(), LoginUtil.COOKIE_FRONT);

		// BackOperator bo = backOperatorManager.login(backOperatorDto.getExactUserName(),
		// backOperatorDto.getPassword(), defaultAdminSiteId);

		// return SUCCESS;

		return SUCCESS;
	}

	public boolean checkLogin(String userName, String password){
		return userManager.findUserByEmailOrMobile(userName, password) != null;
	}

	public void setUserManager(UserManager userManager){
		this.userManager = userManager;
	}

}
